/**
 * 
 */
package com.net.shop.car.manager.ui.personalcenter;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.net.shop.car.manager.App;
import com.net.shop.car.manager.BaseFragment;
import com.net.shop.car.manager.R;
import com.net.shop.car.manager.api.model.Member;
import com.net.shop.car.manager.api.volley.VolleyCenter;
import com.net.shop.car.manager.api.volley.VolleyListenerImpl;
import com.net.shop.car.manager.api.volley.request.dingdan.GetDingdanOrder;
import com.net.shop.car.manager.api.volley.response.dingdan.DingdanOrderResponse;
import com.net.shop.car.manager.ui.MainActivity;
import com.net.shop.car.manager.ui.html.WebviewDialog;
import com.net.shop.car.manager.ui.personalcenter.dingdan.DingdanDialog;
import com.net.shop.car.manager.ui.personalcenter.jifen.JiFenDuiHuanDialog;
import com.net.shop.car.manager.ui.personalcenter.logout.LogoutDialog;
import com.net.shop.car.manager.ui.personalcenter.msg.MyMessageDialog;
import com.net.shop.car.manager.ui.personalcenter.oil.OildCardDialog;
import com.net.shop.car.manager.ui.personalcenter.settings.ReSetPsdDialog;
import com.net.shop.car.manager.ui.personalcenter.share.ShareDialog;
import com.net.shop.car.manager.ui.user.LoginActivity;
import com.net.shop.car.manager.utils.LogUtils;

/**
 * 个人中心
 * 
 * @author Deve
 * 
 */
public class PersonalCenterFragment extends BaseFragment {

	/**
	 * 
	 */
	private Dialog curerntDialog;

	/**
	 * 
	 */
	private ImageView headIcon;

	/**
	 * 
	 */
	private TextView userName;

	private TextView vb;

	private Member user;
	
	/**
	 * 
	 */
	private TextView logView;

	/**
	 * 
	 */
	public PersonalCenterFragment() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_personalcenter,
				container, false);
		initViews(view);
		return view;
	}

	/**
	 * 
	 */
	private void init() {
		user = App.i().getUser();
		if (user != null && !App.i().needLogin) {
			if (user.getHeadIcon() != null) {
				headIcon.setImageBitmap(user.getHeadIcon());
			} else {
				VolleyCenter.getVolley().addImageRequest(user.getPicUrl(),
						new Listener<Bitmap>() {

							@Override
							public void onResponse(Bitmap response) {
								user.setHeadIcon(response);
							}

						}, new Response.ErrorListener() {

							@Override
							public void onErrorResponse(VolleyError error) {

							}
						});
			}
			if (!TextUtils.isEmpty(user.getName())) {
				userName.setText(user.getName());
			} else {
				userName.setText(user.getMobile());
			}
			vb.setText(user.getVb() + "");
			logView.setText("退出登录");
		} else {
			userName.setText("未登录");
			vb.setText("0");
			logView.setText("登录");
		}
	}

	/**
	 * @param view
	 *            View
	 */
	private void initViews(View view) {

		headIcon = (ImageView) view
				.findViewById(R.id.frag_personalcenter_headicon);
		userName = (TextView) view
				.findViewById(R.id.frag_personalcenter_account);
		vb = (TextView) view
				.findViewById(R.id.frag_personalcenter_jifen_digital);

		view.findViewById(R.id.personal_oil).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						gotoOilManager();
					}
				});
		view.findViewById(R.id.personal_jifen).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						gotoJifen();
					}
				});
		view.findViewById(R.id.personal_share).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						gotoShare();
					}
				});
		view.findViewById(R.id.personal_dingdan).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						gotoMyDingdan();
					}
				});
		view.findViewById(R.id.frag_personalcenter_msg).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						gotoMyMsg();
					}
				});
		view.findViewById(R.id.personal_update).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						gotoUpdate();
					}
				});
		view.findViewById(R.id.personal_about).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						gotoAbout();
					}
				});
		view.findViewById(R.id.frag_personalcenter_chongzhi).setOnClickListener(
				new View.OnClickListener() {
					
					@Override
					public void onClick(View v) {
						ChongZhiDialog czDialog = new ChongZhiDialog(holder, vb.getText().toString());
						czDialog.show();
					}
				});
		view.findViewById(R.id.personal_psd_modify).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						gotoPsdModify();
					}
				});

		logView = (TextView) view.findViewById(R.id.personal_logout);
		logView.setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						logout();
					}
				});

		init();
	}

	@Override
	public void onHiddenChanged(boolean hidden) {
		super.onHiddenChanged(hidden);
		if (selfVisible) {
			init();
		}
	}

	/**
	 * 油卡管理
	 */
	public void gotoOilManager() {
		if (checkLogin()) {
			if (curerntDialog != null && curerntDialog.isShowing()) {
				return;
			}
			curerntDialog = new OildCardDialog(holder,user);
			curerntDialog.show();
		}
	}

	/**
	 * 关于
	 */
	public void gotoAbout() {
		if (curerntDialog != null && curerntDialog.isShowing()) {
			return;
		}
		WebviewDialog webviewDialog = new WebviewDialog(holder, "关于车联");
		webviewDialog.setUrl("http://tapi.clejw.com/app/about.jsp");
		curerntDialog = webviewDialog;
		curerntDialog.show();
	}

	/**
	 * 登出
	 */
	public void logout() {
		if (curerntDialog != null && curerntDialog.isShowing()) {
			return;
		}
		if (App.i().needLogin) {
			holder.actionStart(LoginActivity.class);
			return;
		}
		LogoutDialog logoutDialog = new LogoutDialog(holder, "退出登录");
		logoutDialog.setDes("您确定要登出吗？\n登出后将回到登录页面");
		logoutDialog.setDialogListener(new LogoutDialog.DialogListener() {

			@Override
			public void onOkClicked() {
				App.i().logout();
				holder.actionStart(LoginActivity.class);
				holder.finish();
			}
		});
		curerntDialog = logoutDialog;
		curerntDialog.show();
	}

	/**
	 * 密码修改
	 */
	public void gotoPsdModify() {
		if (checkLogin()) {
			if (curerntDialog != null && curerntDialog.isShowing()) {
				return;
			}
			curerntDialog = new ReSetPsdDialog(holder);
			curerntDialog.show();
		}
	}

	/**
	 * 积分兑换
	 */
	public void gotoJifen() {
		if (checkLogin()) {
			if (curerntDialog != null && curerntDialog.isShowing()) {
				return;
			}
			curerntDialog = new JiFenDuiHuanDialog(holder, user);
			curerntDialog.show();
		}
	}

	/**
	 * 分享有礼
	 */
	public void gotoShare() {
		ShareDialog shareDialog = new ShareDialog(holder);
		shareDialog.show();
	}

	/**
	 * 我的订单
	 */
	public void gotoMyDingdan() {
		if (checkLogin()) {
			if (holder.progressDialog.isShowing()) {
				return;
			}
			holder.progressDialog.show();
			VolleyCenter.getVolley().addGetRequest(
					new GetDingdanOrder(user.getMemberId()),
					new VolleyListenerImpl<DingdanOrderResponse>(
							new DingdanOrderResponse()) {

						@Override
						public void deleveryResponse(
								DingdanOrderResponse response) {
							holder.progressDialog.dismiss();
							if (response.isSuccess()) {
								DingdanDialog dialog = new DingdanDialog(
										holder, user);
								dialog.setDingNum(response.getDingdans());
								dialog.show();
							}
						}
					});
		}
	}

	/**
	 * 个人消息
	 */
	public void gotoMyMsg() {
		if (checkLogin()) {
			if (curerntDialog != null && curerntDialog.isShowing()) {
				return;
			}
			curerntDialog = new MyMessageDialog(holder);
			curerntDialog.show();
		}
	}

	/**
	 * 版本更新
	 */
	public void gotoUpdate() {
		if (curerntDialog != null && curerntDialog.isShowing()) {
			return;
		}
	}

	private boolean checkLogin() {
		if (App.i().needLogin || user == null) {
			holder.gotoLogin();
			return false;
		}
		return true;
	}

}
